public class code6 {
//    top 101 删除链表中重复元素 II
public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head==null) {
            return null;
        }
        //创建一个节点，指向头结点 -1->head
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        //用来记录不相同节点的前一个节点 例如1->2->2 则pre = 1;
        ListNode pre = dummy;
        ListNode cur = head;
        while(cur!=null && cur.next!=null) {
            if(cur.next.val!=cur.val) {
                pre = cur;
                cur = cur.next;
            }else {
                ListNode n = cur.next.next;
                int val = cur.val;
                //循环遍历 直到遇到不重复的节点
                while(n!=null) {
                    if(n.val!=val) {
                        break;
                    }
                    n = n.next;
                }
                pre.next = n;
                cur = n;
            }

        }
        return dummy.next;
    }
}
